import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { get_classlist,get_manger_grade} from '@/services/index';
export interface ClassListModelState {
    listexam:any,
    mangerlist:any
}

export interface ClassListModelType {
    namespace: 'exam';
    state: ClassListModelState;
    effects: {
        getstudent: Effect; 
        getmangergrade:Effect;       
    };
    reducers: {
        save: Reducer<ClassListModelState>;
    }
    subscriptions: { setup: Subscription };
    
}

const UserModel: ClassListModelType = {
    namespace: 'exam',
    state: {
        listexam:[],
        mangerlist:[]
    },

    effects: {
        *getstudent({ payload }, { call, put }) {
            let result = yield call(get_classlist,payload);
            console.log(result);
            if (result.code === 1) {
                yield put({
                    type:'save',
                    payload:{listexam:result.exam}
                })
            }
        },
        *getmangergrade({payload},{call,put}){
            let result = yield call(get_manger_grade);
            console.log(result);
            if (result.code === 1) {
                yield put({
                    type:'save',
                    payload:{mangerlist:result.data}
                })
            }
        }
    },
    reducers:{
        save(state,action){
            console.log(state);
            
            return{
                ...state,
                ...action.payload,
            }
        }
    },
    subscriptions:{
        setup({dispatch,history}){
            return history.listen(({pathname})=>{
                if(pathname==='/'){
                    dispatch({
                        type:'exam',
                    })
                }
            });
        }
    }
};

export default UserModel;